// 认关路由配置

const Router = require('@koa/router');
const mongoose = require('mongoose');
const LogModel = mongoose.model('Log');

const router = new Router({
  prefix: '/log',
});

// 日志列表
router.get('/list', async (ctx) => {
  let { page, size } = ctx.query;
  page = Number(page);
  size = Number(size);
  const list = await LogModel.find({ show: true })
    .sort({ _id: -1 })
    .skip((page - 1) * size)
    .limit(size)
    .exec();
  const total = await LogModel.find().countDocuments().exec();
  ctx.body = {
    msg: '获取操作日志列表成功',
    code: 1,
    data: {
      total,
      list,
    },
  };
});

// 删除日志
router.post('/delete', async (ctx) => {
  const { id } = ctx.request.body;
  const delMsg = await LogModel.deleteOne({ _id: id }).exec();
  ctx.body = {
    msg: '删除日志成功',
    code: 1,
    data: delMsg,
  };
});

module.exports = router;
